const request = require('request');
const fs = require('fs');
const path = require('path');
//#下载文件
module.exports.downloadFile = downloadFile;
async function downloadFile(url, dir, callback) {
    //#创建临时文件夹存放本次操作的文件
    mkdirSync('./static/file/' + dir);
    mkdirSync('./static/pdf/' + dir);
    mkdirSync('./static/img/' + dir);
    let filename = url.slice(url.lastIndexOf('/'));
    let stream = fs.createWriteStream('./static/file/' + dir + filename);
    request(url).pipe(stream).on('close', callback);
}


module.exports.mkdirSync = mkdirSync;
async function mkdirSync(dirname) {
    if (fs.existsSync(dirname)) {
        return true;
    } else {
        if (mkdirSync(path.dirname(dirname))) {
            fs.mkdirSync(dirname);
            return true;
        }
    }
    return false;
}